package edu.hawaii.myisern.model;

/**
 * User object to login to MyIsern system.
 * Adapted from bugzooky example.
 * 
 * @author Tim Fennell
 * @author Brian Goetz
 * @author Lisa Chen
 */
public class User {

  /** User login name. */
  private String username;
  /** User login password. */
  private String password;
  /** User id number. */
  private Integer id;
  /** Initial value for hash, used in hashCode Override method. */
  private static final int HASH_BASE_VALUE = 31;
  
  /**
   * Default constructor.
   */
  public User() {
    // do nothing
  }
  
  /**
   * Create a user constructor.
   * 
   * @param username User's login name.
   * @param password User's login password.
   */
  public User(String username, String password) {
    this.username = username;
    this.password = password;
  }
  
  /** 
   * Gets the user ID.
   * 
   * @return Integer user id number.
   */
  public Integer getId() {
    return id;
  }

  /** 
   * Sets the user ID.
   * 
   * @param id Integer user id number.
   */
  public void setId(Integer id) {
    this.id = id;
  }
  
  /**
   * Set user's login name.
   * 
   * @param username User's login name.
   */
  public void setUsername(String username) {
    this.username = username;
  }
  
  /**
   * Get the user's login name.
   * 
   * @return User's login name.
   */
  public String getUsername() {
    return this.username;
  }
  
  /**
   * Set user's login password.
   * 
   * @param password User's login password.
   */
  public void setPassword(String password) {
    this.password = password;
  }
   
  /**
   * Get user's login password.
   * 
   * @return User's password.
   */
  public String getPassword() {
    return this.password;
  }
  
  /** 
   * Override equality method.
   * User login validation is determined by ID number equality.
   * 
   * @see http://blog.palantirtech.com/2007/09/02/compareto-consistent-with-equals/
   * @param obj to compare to this user.
   * @return True if the obj is equal to the user.
   */
  @Override
  public boolean equals(Object obj) {
    if (obj == null) {
      return false;
    }
    else if (obj instanceof User) {
      return (obj instanceof User) && this.id == ((User) obj).id;      
    }
    else {
      return false;
    }
  }
  
  /** 
   * Override hashCode method.
   * User login validation is determined by ID number equality.
   * 
   * @see http://www.ibm.com/developerworks/java/library/j-jtp05273.html
   * @return int hash result.
   */
  @Override
  public int hashCode() {
    int hash = HASH_BASE_VALUE;
    hash = hash * HASH_BASE_VALUE + this.id;
    return hash;
  }
}
